package org.deltaset.meta.faces;

import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import org.deltaset.faces.FacesUtils;
import org.deltaset.meta.extension.RepositoryProvider;

/**
 *
 * @author dmakariev
 */
class FacesStateRepository {
    private final UIComponent uic;
    
    private static final Logger logger = Logger.getLogger(FacesStateRepository.class.getName());
        private static final String RECORD_ID = "recordId";
        private static final String HISTORY_STACK_KEY = "history"+ MetaCrudNamingContainer.class.getName();

    FacesStateRepository(UIComponent uic) {
        this.uic = uic;
    }
    
    HistoryStack getHistoryStack(){
        final HistoryStack historyStack = (HistoryStack)FacesUtils.getViewScope().get(HISTORY_STACK_KEY);
        if(null!=historyStack){
            return historyStack;
        }
        final HistoryStack newHistoryStack = new HistoryStack();
        FacesUtils.getViewScope().put(HISTORY_STACK_KEY, newHistoryStack);
        return newHistoryStack;
    }

    void putMetaEntryState(final Class entityClass, final MetaEntryState metaEntryState) {
        final String stateKey = entityClass.getName() + MetaEntryState.class.getName();
        FacesUtils.getViewScope().put(stateKey, metaEntryState);
    }

    MetaEntryState getMetaEntryState(final Class entityClass) {
        final String stateKey = entityClass.getName() + MetaEntryState.class.getName();
        return (MetaEntryState) FacesUtils.getViewScope().get(stateKey);
    }

    RepositoryProvider getValueRepositoryProvider() {
        return FacesUtils.getAttribute(this.uic, "repositoryProvider", RepositoryProvider.class);
    }

    String getValueCalatogName() {
        return FacesUtils.getAttribute(this.uic, "catalogName");
    }

    String getValueEntityClass() {
        return FacesUtils.getAttribute(this.uic, "entityClass");
    }
    
    String getRequestEntityClass() {
        return FacesUtils.getParameterString("entityClass");
    }

    String getRequestRecordId() {
        return FacesUtils.getParameterString(RECORD_ID);
    }

    String getRequestShowView() {
        return FacesUtils.getParameterString("showView");
    }

    String getAttributeOrException(final String attributeName) {
        final String attributeValue = FacesUtils.getAttribute(this.uic, attributeName);
        if (null == attributeValue) {
            logger.log(Level.SEVERE, "{0} must be defined for {1}", new Object[]{attributeName, this.getClass().getName()});
            throw new IllegalArgumentException(attributeName + " must be defined for " + this.getClass().getName());
        }
        if (attributeValue.length() < 1) {
            logger.log(Level.SEVERE, "{0} cannot be empty string for {1}", new Object[]{attributeName, this.getClass().getName()});
            throw new IllegalArgumentException(attributeName + " cannot be empty string for " + this.getClass().getName());
        }
        return attributeValue;
    }

    boolean getHasValidationErrors() {
        return FacesMessage.SEVERITY_ERROR.equals(FacesContext.getCurrentInstance().getMaximumSeverity());
    }

    Locale getCurrentLocale() {
        final Locale currentLocale = FacesContext.getCurrentInstance().getViewRoot().getLocale();
        return currentLocale;
    }

    void addInfoMessage(final String message) {
        addMessage(FacesMessage.SEVERITY_INFO, message);
    }

    void addErrorMessage(final String message) {
        addMessage(FacesMessage.SEVERITY_ERROR, message);
    }

    private void addMessage(final FacesMessage.Severity severity, final String msg) {
        final FacesMessage facesMsg = new FacesMessage(severity, msg, msg);
        FacesContext.getCurrentInstance().addMessage(null, facesMsg);
    }
    
}
